
#import "SMPostCell.h"
#import "SMPost.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/UIImageView+WebCache.h>

@interface SMPostCell ()
@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) UILabel *contentLabel;
@property(nonatomic, strong) UIImageView *thumbView;
@end

@implementation SMPostCell
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.contentView.backgroundColor = UIColor.secondarySystemBackgroundColor;
        self.contentView.layer.cornerRadius = 12;
        self.contentView.layer.masksToBounds = YES;

        _titleLabel = [UILabel new];
        _titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _contentLabel = [UILabel new];
        _contentLabel.font = [UIFont systemFontOfSize:14];
        _contentLabel.numberOfLines = 2;
        _thumbView = [UIImageView new];
        _thumbView.contentMode = UIViewContentModeScaleAspectFill;
        _thumbView.clipsToBounds = YES;

        [self.contentView addSubview:_thumbView];
        [self.contentView addSubview:_titleLabel];
        [self.contentView addSubview:_contentLabel];

        [_thumbView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(self.contentView);
            make.width.equalTo(self.contentView.mas_height);
        }];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(10);
            make.left.equalTo(self->_thumbView.mas_right).offset(10);
            make.right.equalTo(self.contentView).offset(-10);
        }];
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->_titleLabel.mas_bottom).offset(6);
            make.left.right.equalTo(self->_titleLabel);
            make.bottom.lessThanOrEqualTo(self.contentView).offset(-10);
        }];
    }
    return self;
}
- (void)prepareForReuse { [super prepareForReuse]; [_thumbView sd_cancelCurrentImageLoad]; _thumbView.image = nil; }
- (void)configureWithPost:(SMPost *)post {
    _titleLabel.text = post.title;
    _contentLabel.text = post.content;
    if (post.imageURL.length) [_thumbView sd_setImageWithURL:[NSURL URLWithString:post.imageURL]];
}
@end
